/*
 * 
 */
package nor;

import java.awt.Color;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;
//import nor.TPoint;

/**
 *
 * @author mignat
 */
public class TPoint extends Point3d {

    private Color theCol = Color.WHITE;
    private Color prawdziwyKolor = null;
    private Vector3d theNorVec = new Vector3d(0.0, 0.0, 0.0);

    TPoint(double aX, double aY, double aZ) {
        super(aX, aY, aZ);
    } //koniec konstruktora

    public TPoint(TPoint aPt) {
        super(aPt.x, aPt.y, aPt.z);
        theCol = aPt.getColor();
        theNorVec = aPt.getNorVec();
    } //koniec TPoint

    //Do tworzenia punktów pomocniczych okalających dany pkt
    TPoint(double aX, double aY, double aZ, Color aCol) {
        super(aX, aY, aZ);
        theCol = aCol;
    } //koniec konstruktora do pktów pomocniczych

    public void calcNormals(TPoint aPtA, TPoint aPtB, TPoint aPtC, TPoint aPtD)  {

        Vector3d voa = new Vector3d(x - aPtA.x, y - aPtA.y, z - aPtA.z);
        Vector3d vob = new Vector3d(x - aPtB.x, y - aPtB.y, z - aPtB.z);
        Vector3d voc = new Vector3d(x - aPtC.x, y - aPtC.y, z - aPtC.z);
        Vector3d vod = new Vector3d(x - aPtD.x, y - aPtD.y, z - aPtD.z);

        // Liczę normalne wzgl pktów AOB, BOC, COD, DOA
        Vector3d vrAB = new Vector3d();
        vrAB.cross(voa, vob);
        if(vrAB.length() != 0.0) vrAB.normalize();
        
        //BOC
        Vector3d vrBC = new Vector3d();
        vrBC.cross(vob, voc);
        if(vrBC.length() != 0.0) vrBC.normalize();
        
        //COD
        Vector3d vrCD = new Vector3d();
        vrCD.cross(voc, vod);
        if(vrCD.length() != 0.0) vrCD.normalize();
        
        //DOA
        Vector3d vrDA = new Vector3d();
        vrDA.cross(vod, voa);
        if(vrDA.length() != 0.0) vrDA.normalize();
        
        /*
        System.out.println("vrAB: " + vrAB);
        System.out.println("vrBC: " + vrBC);
        System.out.println("vrCD: " + vrCD);
        System.out.println("vrDA: " + vrDA);
         */
        
//---
        double _sreX = (vrAB.x);
        double _sreY = (vrAB.y);
        double _sreZ = (vrAB.z);
        theNorVec = new Vector3d(_sreX, _sreY, _sreZ);   
//===
/* bo liczy za dobrze
        // Liczę srednia
        double _sreX = (vrAB.x + vrBC.x + vrCD.x + vrDA.x) / 4d;
        double _sreY = (vrAB.y + vrBC.y + vrCD.y + vrDA.y) / 4d;
        double _sreZ = (vrAB.z + vrBC.z + vrCD.z + vrDA.z) / 4d;
        theNorVec = new Vector3d(_sreX, _sreY, _sreZ);
*/

        
        int r = (int) (255 * Math.abs(theNorVec.x));
        int g = (int) (255 * Math.abs(theNorVec.y));
        int b = (int) (255 * Math.abs(theNorVec.z));
        
        this.prawdziwyKolor = new Color(r,g,b);
 
        //---
         if(r > g && r > b) {
            r = 255;    g = 255;   b = 255;
        } else if(g > r && g > b) {
            r = 128;      g = 128;    b = 128;
        } else if(b > r && b > g) {
            r = 70;      g = 70;      b = 70;
        } else if(g == r && g > b) {
            r = 0;    g = 0;    b = 0;
        } else if(b == r && b > g) {
            r = 0;    g = 0;      b = 0;
        } else if(g == b && g > r) {
            r = 0;  g = 0;    b = 0;
        }
        
        
        /*
        //Biorę kolor domiunujacy
        if(r > g && r > b) {
            r = 255;    g = 0;      b = 0;
        } else if(g > r && g > b) {
            r = 0;      g = 255;    b = 0;
        } else if(b > r && b > g) {
            r = 0;      g = 0;      b = 255;
        } else if(g == r && g > b) {
            r = 255;    g = 255;    b = 0;
        } else if(b == r && b > g) {
            r = 255;    g = 0;      b = 255;
        } else if(g == b && g > r) {
            r = 0;  g = 255;    b = 255;
        }
*/
        //===
        theCol = new Color(r, g, b);

    } //koniec calcNormals
    /*
    public void calcNormals (TPoint aPtB, TPoint aPtC) {
    
    Vector3d vr = new Vector3d();
    Vector3d vab = new Vector3d(x - aPtB.x, y - aPtB.y, z - aPtB.z);
    Vector3d vac = new Vector3d(x - aPtC.x, y - aPtC.y, z - aPtC.z);
    vr.cross(vab , vac);
    
    vr.normalize();
    
    int r = (int)(255*Math.abs(vr.x));
    int g = (int)(255*Math.abs(vr.y));
    int b = (int)(255*Math.abs(vr.z));
    
    //---
    //Biorę kolor domiunujacy
    if (r>g && r>b) { r=255; g=0; b=0;}
    else if (g>r && g>b) { r=0; g=255; b=0;}
    else if (b>r && b>g) { r=0; g=0; b=255;}
    else if (g==r && g>b) { r=255; g=255; b=0;}
    else if (b==r && b>g) { r=255; g=0; b=255;}
    else if (g==b && g>r) { r=0; g=255; b=255;}
    //===
    theCol = new Color (r, g, b);
    } //koniec calcNormals
     */
    public void setColor(Color aCol) {
        theCol = aCol;
    }

    public Color getColor() {
        return theCol;
    }
    public Color getPrawdziwyKolor(){
      return this.prawdziwyKolor;
    }

    public Vector3d getNorVec() {
        return this.theNorVec;
    }
    
    public double odlegloscKW(Point3d p){
        return    (this.x - p.x)*(this.x - p.x) 
                + (this.y - p.y)*(this.y - p.y) 
                + (this.z - p.z)*(this.z - p.z); 
    }
  
} //koniec klasy TPoint
